﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using System.Globalization;
using Pheonix.CodeLibrary.Web;
using Pheonix.Fashionzoomer.BLL;
using System.IO;
using Pheonix.Fashionzoomer.DAL;
using System.Security.Cryptography;

public partial class page_public_productdetail : BasePage
{
    public string maxViewImage1, maxViewImage2, maxViewImage3, maxViewImage4;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - Product Details";
        addToFavoriteButton.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/addToFav.png";
        fullSizeView.Src = "~/App_Themes/" + Page.StyleSheetTheme + "/image/viewFullSize.png";

        maxViewPrev.Src = "~/App_Themes/" + Page.StyleSheetTheme + "/image/maxview_prev.gif";
        maxViewNext.Src = "~/App_Themes/" + Page.StyleSheetTheme + "/image/maxview_next.gif";
        
        LoadData();

    }

    private void LoadData()
    {
        if (!String.IsNullOrEmpty(Request.QueryString["id"]))
        {
            Product product = null;

            int id = 0;

            if(Int32.TryParse(Request.QueryString["id"], out id))
                product = Product.GetProductById(id);

            if (product != null)
            {
                Shop shop = Shop.GetShopById(product.ShopId);

                bigView.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/bigImageNA.gif";
                thumbView1.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/thumbNA.gif";
                thumbView2.ImageUrl = thumbView3.ImageUrl = thumbView4.ImageUrl = thumbView1.ImageUrl;
                maxViewImage1 = ResolveUrl("~/App_Themes/" + Page.StyleSheetTheme + "/image/bigImageNA.gif");
                maxViewImage2 = maxViewImage3 = maxViewImage4 = maxViewImage1;

                string bigImageUrl = product.DefaultImageUrl;
                string thumb1ImageUrl = product.DefaultImageUrl.Replace("mid", "thumb");
                string thumb2ImageUrl = product.DefaultImageUrl.Replace("mid_1", "thumb_2");
                string thumb3ImageUrl = product.DefaultImageUrl.Replace("mid_1", "thumb_3");
                string thumb4ImageUrl = product.DefaultImageUrl.Replace("mid_1", "thumb_4");
                string max1ImageUrl = ResolveUrl(product.DefaultImageUrl.Replace("mid", "max"));
                string max2ImageUrl = ResolveUrl(product.DefaultImageUrl.Replace("mid_1", "max_2"));
                string max3ImageUrl = ResolveUrl(product.DefaultImageUrl.Replace("mid_1", "max_3"));
                string max4ImageUrl = ResolveUrl(product.DefaultImageUrl.Replace("mid_1", "max_4"));

                if (File.Exists(Server.MapPath(bigImageUrl)))
                    bigView.ImageUrl = bigImageUrl;

                if (File.Exists(Server.MapPath(thumb1ImageUrl)))
                    thumbView1.ImageUrl = thumb1ImageUrl;

                if (File.Exists(Server.MapPath(thumb2ImageUrl)))
                    thumbView2.ImageUrl = thumb2ImageUrl;

                if (File.Exists(Server.MapPath(thumb3ImageUrl)))
                    thumbView3.ImageUrl = thumb3ImageUrl;

                if (File.Exists(Server.MapPath(thumb4ImageUrl)))
                    thumbView4.ImageUrl = thumb4ImageUrl;


                if (File.Exists(Server.MapPath(max1ImageUrl)))
                    maxView.ImageUrl = max1ImageUrl;
                if(File.Exists(Server.MapPath(max1ImageUrl)))
                    maxViewImage1 = max1ImageUrl;
                if(File.Exists(Server.MapPath(max2ImageUrl)))
                    maxViewImage2 = max2ImageUrl;
                if(File.Exists(Server.MapPath(max3ImageUrl)))
                    maxViewImage3 = max3ImageUrl;
                if(File.Exists(Server.MapPath(max4ImageUrl)))
                    maxViewImage4 = max4ImageUrl;

                productName.Text = product.ProductName;
                brandName.Text = shop.ShopName;
                brandName.NavigateUrl = "shopdetail.aspx?id=" + shop.Id;
                price.Text = product.PriceLow.ToString() + "tk. - " + product.PriceHigh.ToString() + "tk.";
                description.Text = product.ProductDescription;
                available.Text = shop.BranchAddress;

                rating.Text = product.Rating.ToString();
                views.Text = product.TotalView.ToString();

                availableFrom.Text = product.AvailableFrom.ToString("dd MMM yyyy",CultureInfo.CreateSpecificCulture("en-UK"));

                DataTable relatedProducts = ProductManager.GetRelatedProductList(product.Id, product.CategoryId);

                Image[] relatedImages = new Image[8];
                relatedImages[0] = relatedImage1;
                relatedImages[1] = relatedImage2;
                relatedImages[2] = relatedImage3;
                relatedImages[3] = relatedImage4;
                relatedImages[4] = relatedImage5;
                relatedImages[5] = relatedImage6;
                relatedImages[6] = relatedImage7;
                relatedImages[7] = relatedImage8;

                HyperLink[] relatedLinks = new HyperLink[8];
                relatedLinks[0] = relatedImageLink1;
                relatedLinks[1] = relatedImageLink2;
                relatedLinks[2] = relatedImageLink3;
                relatedLinks[3] = relatedImageLink4;
                relatedLinks[4] = relatedImageLink5;
                relatedLinks[5] = relatedImageLink6;
                relatedLinks[6] = relatedImageLink7;
                relatedLinks[7] = relatedImageLink8;

                relatedImages[0].Visible = relatedLinks[0].Visible = false;
                relatedImages[1].Visible = relatedLinks[1].Visible = false;
                relatedImages[2].Visible = relatedLinks[2].Visible = false;
                relatedImages[3].Visible = relatedLinks[3].Visible = false;
                relatedImages[4].Visible = relatedLinks[4].Visible = false;
                relatedImages[5].Visible = relatedLinks[5].Visible = false;
                relatedImages[6].Visible = relatedLinks[6].Visible = false;
                relatedImages[7].Visible = relatedLinks[7].Visible = false;

                if (relatedProducts != null && relatedProducts.Rows.Count > 0)
                {
                    for (int i = 0; i < relatedProducts.Rows.Count; i++)
                    {
                        if (relatedProducts.Rows[i]["default_image_url"] != null)
                        {
                            relatedImages[i].ImageUrl = relatedProducts.Rows[i]["default_image_url"].ToString().Replace("mid", "thumb");
                            relatedImages[i].Visible = true;
                        }

                        if (relatedProducts.Rows[i]["id"] != null)
                        {
                            relatedLinks[i].NavigateUrl = "productdetail.aspx?id=" + relatedProducts.Rows[i]["id"].ToString();
                            relatedLinks[i].Visible = true;
                        }
                    }
                }
                else
                    pnlRelatedProduct.Visible = false;
            }
        }
    }

    protected void AddToFavorite_Click(object sender, ImageClickEventArgs e)
    {
        bool success = false;
        if (SessionManager.IsUserLoggedIn)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                Product product = null;

                int productId = 0;
                int userId = SessionManager.LoggedUser.Id;

                Int32.TryParse(Request.QueryString["id"], out productId);

                if (userId > 0 && productId > 0)
                {
                    bool checkAlreadyAdded = CustomCommand.IsAlreadyAddedInFavorite(userId, productId);
                    if (!checkAlreadyAdded)
                    {
                        int newFavoriteId = Favorites.CreateNewFavorites(productId, DateTime.Now, userId);
                        if (newFavoriteId > 0)
                            success = true;
                    }
                    else
                        uccResponseMessage.SetFailMessage("This product is already in your favorite list");
                }
            }
        }
        else
            uccResponseMessage.SetFailMessage("Please login first");

        if (success)
        {
            uccResponseMessage.SetSuccessMessage("Added to your favorites");
        }
    }
}
